package net.sparrow.module;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import net.sparrow.Sparrow;
import net.sparrow.cmd.CommandHandler;
import net.sparrow.data.UtilFile;
import net.sparrow.event.DataField;
import net.sparrow.event.SpEventListener;
import net.sparrow.event.SpEvent;
import net.sparrow.util.Util;

public class Module {

	/**
	 * initialize the commandhandler 
	 * for this module.
	 * @return
	 */
	public CommandHandler loadCommands() {
		return new CommandHandler();
	}
	
	
	/**
	 * If the CommandHandler doesn't have an entry for a command,
	 * this command will run instead.
	 * @param args
	 * @return whether or not this command is a valid command
	 */
	public boolean onCommand(String[] args) {
		return false;
	}
	
	

	/**
	 * Loads every classfile in the net.sparrow.module class automatically
	 * and registers its listeners
	 */
	public static void loadModules() {
		Util.print("Starting Module Loading...");
		Class<Module> someModule = Module.class; //this makes the ClassLoader aware of the package "net.sparrow.module"
		ArrayList<Class<?>> moduleList = UtilFile.getClassesForPackage(Package.getPackage("net.sparrow.module"));
		//TODO add modules in packages like net.sparrow.module.pvp or other stuff
		for( Class c : moduleList) {
			try {
			Module module = (Module)c.newInstance();
			Sparrow.data().addModule(module);
			} catch(Exception e) {
				e.printStackTrace();
				Util.print("Failed to load modules! \n" + e.getStackTrace());
			}
		}
	}
}
